﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseAccessLayer;
using System.IO;

namespace WebApplication
{
    public partial class Real_Estate_Post_Offer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CountryService service = new CountryService();
                List<string> country = service.GetCountry();
                ddlCountry.DataSource = country;
                ddlCountry.DataBind();
                ddlCountry.Items.Insert(0, "Country");

                List<string> property = service.GetProperty();
                ddlProperty.DataSource = property;
                ddlProperty.DataBind();
                ddlProperty.Items.Insert(0, "Category");

                List<string> price = service.GetPriceMode();
                ddlPriceMode.DataSource = price;
                ddlPriceMode.DataBind();
                ddlPriceMode.Items.Insert(0, "Mode");

            }
            ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("divbreadcrum")).InnerHtml = @"<a href='default.aspx'>Home</a> >  <a href='menu.aspx'>Menu</a> > <span>Post Offer</span>";

        }
        public void err(string mess)
        {

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language='javascript'>");
            sb.Append(@"alert('" + mess + "');");
            sb.Append(@"</script>");
            if (!ClientScript.IsStartupScriptRegistered("jsscript"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "jsscript", sb.ToString());
            }
        }
        protected void btn_Save(object sender, EventArgs e)
        {
            if (chkNewsLatter.Checked == true)
            {
                PostOfferSt st = new PostOfferSt();
                st.Country = Request.Form.GetValues("ctl00$MainContent$ddlCountry")[0];
                st.Neighbourhood = txtNeighborhood.Text.Trim();
                st.City = Request.Form.GetValues("ctl00$MainContent$ddlCity")[0];
                st.Property = Request.Form.GetValues("ctl00$MainContent$ddlProperty")[0];
                st.PropertyType = Request.Form.GetValues("ctl00$MainContent$ddlPropertyType")[0];
                st.Area = txtArea.Text.Trim();
                st.BathRoom = ddlBathRooms.SelectedItem.Value;
                st.BedRoom = ddlbedrooms.SelectedItem.Value;
                st.LandLineNo = txtLandline.Text.Trim();
                st.PriceMode = ddlPriceMode.SelectedItem.Value;
                st.Price = Convert.ToDecimal(00);
                st.TotalPrice = Convert.ToDecimal(txtTotalPrice.Text.Trim());
                st.UserID = Convert.ToInt32(Session["ID"]);
                st.Picture = upMain.FileName;
                st.Picture1 = up1.FileName;
                st.Picture2 = up2.FileName;
                st.Picture3 = up3.FileName;
                st.Picture4 = up4.FileName;
                int result = PostOffer.InsertPostedOffer(st);
                if (result > 0)
                {
                    Directory.CreateDirectory(Server.MapPath("UploadedFiles/PostOffers/" + result));
                    if (upMain.HasFile)
                    {
                        upMain.PostedFile.SaveAs(Server.MapPath("UploadedFiles/PostOffers/" + result + "/") + upMain.FileName);
                    }
                    if (up1.HasFile)
                    {
                        up1.PostedFile.SaveAs(Server.MapPath("UploadedFiles/PostOffers/" + result + "/") + up1.FileName);
                    }
                    if (up2.HasFile)
                    {
                        up2.PostedFile.SaveAs(Server.MapPath("UploadedFiles/PostOffers/" + result + "/") + up2.FileName);
                    }
                    if (up3.HasFile)
                    {
                        up3.PostedFile.SaveAs(Server.MapPath("UploadedFiles/PostOffers/" + result + "/") + up3.FileName);

                    }
                    if (up4.HasFile)
                    {
                        up4.PostedFile.SaveAs(Server.MapPath("UploadedFiles/PostOffers/" + result + "/") + up4.PostedFile.FileName);
                    }
                    Response.Redirect("Thanks.aspx");
                }
            }
            else
            {
                err("Please Confirm Above Information");
            }
        }
    }
}